package cn.cs.controller;

import cn.cs.common.Dto;
import cn.cs.common.DtoUtil;
import cn.cs.common.EmptyUtils;
import cn.cs.pojo.InvoiceTitle;
import cn.cs.service.InvoiceTitleService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/invoiceTitle")
public class InvoiceTitleController {

    @Resource
    private InvoiceTitleService invoiceTitleService;

    /**
     * 新增发票抬头
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/csPlatform/addInvoiceTitle")
    @Transactional
    public Dto addInvoiceTitle(String name, String taxpayerNumber, String isDefault, String userId) throws Exception {
        if(EmptyUtils.isEmpty(userId)){
            return DtoUtil.returnFail("用户不存在，请重新授权登陆", "userNull001");
        }
        InvoiceTitle invoiceTitle = new InvoiceTitle();
        invoiceTitle.setName(name);
        invoiceTitle.setTaxpayerNumber(taxpayerNumber);
        invoiceTitle.setIsDefault(isDefault);
        invoiceTitle.setUserId(userId);
        if ("1".equals(invoiceTitle.getIsDefault())) {
            invoiceTitleService.setDefaultAll(userId);
        }
        try {
            invoiceTitleService.addInvoiceTitle(invoiceTitle);
            return DtoUtil.returnSuccess("操作成功");
        } catch (Exception e) {
            return DtoUtil.returnFail("操作失败", "002");
        }

    }

    /**
     * 修改
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/csPlatform/editInvoiceTitle")
    public Dto editInvoiceTitle(String name, String taxpayerNumber, String isDefault, String userId, String id) throws Exception {
        InvoiceTitle invoiceTitle = new InvoiceTitle();
        invoiceTitle.setName(name);
        invoiceTitle.setTaxpayerNumber(taxpayerNumber);
        invoiceTitle.setIsDefault(isDefault);
        invoiceTitle.setUserId(userId);
        invoiceTitle.setId(Integer.valueOf(id));
        if ("1".equals(invoiceTitle.getIsDefault())) {
            invoiceTitleService.setDefaultAll(userId);
        }
        invoiceTitleService.modifyInvoiceTitle(invoiceTitle);
        return DtoUtil.returnSuccess("操作成功");
    }

    /**
     * 获取抬头列表
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/p/getInvoiceTitleList")
    public Dto getInvoiceTitleList(String userId) throws Exception {
        if(EmptyUtils.isEmpty(userId)){
            return DtoUtil.returnFail("用户不存在，请重新授权登陆", "userNull001");
        }
        return DtoUtil.returnSuccess("查询成功", invoiceTitleService.getInvoiceTitleListByUserId(userId));
    }

    /**
     * 删除
     *
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/csPlatform/deleteInvoiceTitle/{id}")
    public Dto deleteInvoiceTitle(@PathVariable("id") String id) throws Exception {
        invoiceTitleService.deleteInvoiceTitleById(Long.valueOf(id));
        return DtoUtil.returnSuccess("操作成功");
    }

    /**
     * 设为默认
     */
    @RequestMapping("/p/setDefault/{id}")
    @ResponseBody
    public Dto setDefault(@PathVariable("id") String id) throws Exception {
        int i = invoiceTitleService.setDefault(Long.valueOf(id));
        if (i > 0) {
            return DtoUtil.returnSuccess("操作成功");
        } else {
            return DtoUtil.returnFail("操作失败，请重试", "0002");
        }

    }


    /**
     * 查询默认发票抬头
     */
    @RequestMapping("/p/getDefaultById")
    @ResponseBody
    public Dto getDefaultById(String userId) throws Exception {
        if(EmptyUtils.isEmpty(userId)){
            return DtoUtil.returnFail("用户不存在，请重新授权登陆", "userNull001");
        }
        List<InvoiceTitle> list = invoiceTitleService.getInvoiceTitleListByUserId(userId);
        if(list.size() == 1){
            return DtoUtil.returnSuccess("查询成功", list.get(0));
        }
        InvoiceTitle invoiceTitle = invoiceTitleService.getDefaultByUserId(userId);
        return DtoUtil.returnSuccess("查询成功", invoiceTitle);
    }

}
